﻿using Communergy.Repository;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Communergy.Common;
using System.Collections.Generic;
using System;
using System.Configuration;
using Communergy.UnitTesting;
using Communergy.Repository.EntityFramework;

namespace CurrentCostTest
{
    
    
    /// <summary>
    ///This is a test class for EntityMeterRepositoryTest and is intended
    ///to contain all EntityMeterRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class EntityMeterRepositoryTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetMetersForUser
        ///</summary>
        [TestMethod()]
        public void GetMetersForUserTest()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["communergyEntities"].ConnectionString;
            var efFactory = new EntityRepositoryFactory();
            var unitOfWork = efFactory.CreateUnitOfWork();
            IMeterRepository target = efFactory.CreateMeterRepository(unitOfWork);
            var userGuid = Guid.NewGuid();
            string userId = userGuid.ToString();
            
            List<Meter> initialMeters;
            initialMeters = target.GetMetersForUser(userId);
            Assert.AreEqual(0, initialMeters.Count);

            using (var db = new communergyEntities(connectionString))
            {
                var testMeter = new ElectricityMeter
                {
                    me_meterGuid = Guid.NewGuid(),
                    me_meterName = UnitTestHelper.RandomString(15),
                    me_aspUserId = userGuid

                };
                db.AddToElectricityMeter(testMeter);
                db.SaveChanges();

                var updatedMeters = target.GetMetersForUser(userId);
                Assert.AreEqual(1, updatedMeters.Count);

            }

        }
    }
}
